/**
 * Created by asdzheng on 2016/2/18.
 */

/**
 *  点击的页码数
 * @type {string}
 */
var click_page = "1";

/**
 * 初始化页码和改变页面时需要调用的此函数
 * @param page page的div
 * @param count 总页数
 * @param current 当前页数
 * @param ajax 跳转时是否使用ajax
 * @param callback 点击页码时的回调函数
 * @param dataurl 不使用Ajax时跳转的url
 * @returns {boolean}
 */

function pageChange(page, count, current, ajax, callback, dataurl) {

    var page = page;
    var dataurl = dataurl;
    //字符串转成功int
    var count = count * 1;
    if (count == 0) return false;
    var current = current * 1;
    var ajax = ajax || false;


    $.nmDebug.log("count  = " + count + " current " + current);

    //上一页
    var previous_page;
    //下一页
    var next_page;
    if (!ajax) {
        var next_url = '?page=' + (current + 1);
        var previous_url = '?page=' + (current - 1);
        previous_page = "<li class='previous-page'><a href=" + previous_url + ">上一页</a></li>";
        next_page = "<li class='next-page'><a href=" + next_url + ">下一页</a></li>"
    } else {
        previous_page = "<li data-page_num='" + (current - 1) + "' class='previous-page'><a>上一页</a></li>";
        next_page = "<li data-page_num='" + (current + 1) + "' class='next-page'><a>下一页</a></li>"
    }
    //省略号
    var elip = "<li class='elip-page'><a>···</a></li>"
    if (current != 1) {
        page.append(previous_page);
    }

    if (count < 5) {
        for (var i = 1; i <= count; i++) {
            normal_page(i);
        }
    } else {
        if (current == 1) {
            normal_page(1);
            normal_page(2);
            normal_page(3);
            page.append(elip);
            normal_page(count);
        } else {
            normal_page(1);
            if (current - 3 > 1) {
                page.append(elip);
            }
            if (current - 2 > 1) {
                normal_page(current - 2);
            }
            if (current - 1 > 1) {
                normal_page(current - 1);
            }
            if (current != count) {
                normal_page(current);
            }
            if (current + 1 < count) {
                normal_page(current + 1);
            }
            if (current + 2 < count) {
                normal_page(current + 2);
            }
            if (current + 3 < count) {
                page.append(elip);
            }
            normal_page(count);
        }
    }

    if (current != count) {
        page.append(next_page);
    }

    //鼠标移动过页码的效果
    whiteBtnMouseEvent($(".normal-page"));
    whiteBtnMouseEvent($(".previous-page"));
    whiteBtnMouseEvent($(".next-page"));
    //使用ajax时，点击页码需要回调callback
    if (ajax != false) {
        $(".page li").click(function() {
            //如果是三个点的话点击是无效果的
            if ($(this).attr("class") == "elip-page") {
                return;
            }
            //全局变量，点击的页码数
            click_page = $(this).data("page_num");
            $.nmDebug.log("page_num = " + click_page);
            callback();
        });
    }


    /**
     * 显示普通页面
     * @param page_num
     * @returns {*|void|ServerResponse}
     */
    function normal_page(page_num) {
        var page = $(".page");
        var ajax = page.data("ajax") || false;
        var li;
        var current = page.data("current");
        var class_name = "";
        if (page_num == current) {
            class_name = "active-page";
        } else {
            class_name = "normal-page";
        }


        if (!ajax) {
            var url = dataurl + 'page=' + page_num;
            li = "<li class=" + class_name + "><a href='" + url + "' >" + page_num + "</a></li>"
        } else {
            li = "<li data-page_num='" + page_num + "' class=" + class_name + "><a>" + page_num + "</a></li>"
        }
        return page.append(li);
    }
}
